package cn.qiyu5522.service.impl;

import cn.qiyu5522.constant.SystemConstants;
import cn.qiyu5522.domain.DTO.CategoryPageQueryDTO;
import cn.qiyu5522.domain.VO.CategoryPageQueryVO;
import cn.qiyu5522.domain.VO.CategoryVO;
import cn.qiyu5522.domain.VO.PageVO;
import cn.qiyu5522.domain.entity.Category;
import cn.qiyu5522.domain.entity.ResponseResult;
import cn.qiyu5522.mapper.CategoryMapper;
import cn.qiyu5522.service.CategoryService;
import cn.qiyu5522.utils.BeanCopyUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 分类表 服务实现类
 * </p>
 *
 * @author Tonkey
 * @since 2024-04-24
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Override
    public ResponseResult listAllCategory() {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getStatus, SystemConstants.STATUS_NORMAL);
        List<Category> list = list(queryWrapper);
        // 转 VO
        List<CategoryVO> categoryVOS = BeanCopyUtils.copyBeanList(list, CategoryVO.class);
        return ResponseResult.okResult(categoryVOS);
    }

    @Override
    public ResponseResult getCategoryListByCondition(CategoryPageQueryDTO categoryPageQueryDTO) {
        Integer pageNum = categoryPageQueryDTO.getPageNum();
        Integer pageSize = categoryPageQueryDTO.getPageSize();
        String name = categoryPageQueryDTO.getName();
        String status = categoryPageQueryDTO.getStatus();
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(name),Category::getName,name)
                .eq(StringUtils.hasText(status),Category::getStatus,status);
        Page<Category> page = new Page<>(pageNum,pageSize);
        page(page,queryWrapper);
        List<Category> records = page.getRecords();
        // 转VO
        List<CategoryPageQueryVO> categoryVOS = BeanCopyUtils.copyBeanList(records, CategoryPageQueryVO.class);
        PageVO pageVO = new PageVO(categoryVOS, page.getTotal());
        return ResponseResult.okResult(pageVO);
    }
}
